#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct customer
{
    string name;
    int sh, sm, ss;
    int eh, em, es;
};
bool cmp1(const customer &c1, const customer &c2)
{
    if (c1.sh == c2.sh)
    {
        if (c1.sm == c2.sm)
        {
            return c1.ss < c2.ss;
        }
        else
        {
            return c1.sm < c2.sm;
        }
    }
    else
    {
        return c1.sh < c2.sh;
    }
}
bool cmp2(const customer &c1, const customer &c2)
{
    if (c1.eh == c2.eh)
    {
        if (c1.em == c2.em)
        {
            return c1.es > c2.es;
        }
        else
        {
            return c1.em > c2.em;
        }
    }
    else
    {
        return c1.eh > c2.eh;
    }
}
int main()
{
    int m;
    cin >> m;
    string st, et;
    customer cus[1000];
    for (int i = 0; i < m; i++)
    {
        cin >> cus[i].name >> st >> et;
        cus[i].sh = (st[0] - '0') * 10 + st[1] - '0';
        cus[i].sm = (st[3] - '0') * 10 + st[4] - '0';
        cus[i].ss = (st[6] - '0') * 10 + st[7] - '0';
        cus[i].eh = (et[0] - '0') * 10 + et[1] - '0';
        cus[i].em = (et[3] - '0') * 10 + et[4] - '0';
        cus[i].es = (et[6] - '0') * 10 + et[7] - '0';
    }
    sort(cus,cus + m, cmp1);
    cout << cus[0].name << " ";
    sort(cus,cus + m, cmp2);
    cout << cus[0].name;
    return 0;
}